/*
 *  Copyright 2010 UPF Telecos 4th group G.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package edu.upf.content;

import edu.upf.utils.Definitions;
import java.awt.Image;
import java.io.File;
import java.io.Serializable;
import java.net.URL;
import javax.swing.ImageIcon;

/**
 * This class models a video with an id and a list of keywords
 * @author netjois
 */
public class Video implements Serializable, Definitions, DescriptionContent {

    private String name;
    private String size;
    private String path;
    private String popularity;
    private String owner;
    private String format;
    private ImageIcon thumbnail;
    private String ip;
    private String album;

    public Video() {
        thumbnail = new ImageIcon(getClass().getResource("/thumb_icons/video_default_thumb.png"));
    }

    @Override
    public void setName(String name) {
        this.name = name;
        if (thumbnail != null) {
            thumbnail.setDescription(name);
        }
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setSize(String s) {
        size = s;
    }

    @Override
    public String getSize() {
        return size;
    }

    @Override
    public String getPath() {
        path = "Shared_Folder/" + getAlbum() + "/" + getName() + "." + getFormat();
        return path;
    }

    @Override
    public void setPopularity(String p) {
        popularity = p;
    }

    @Override
    public String getPopularity() {
        return popularity;
    }

    @Override
    public void setOwner(String owner) {
        this.owner = owner;
    }

    @Override
    public String getOwner() {
        return owner;
    }

    @Override
    public void setFormat(String format) {
        this.format = format;
    }

    @Override
    public String getFormat() {
        return format;
    }

    @Override
    public ImageIcon getThumbnail() {
        return thumbnail;
    }

    /**
     * Sets the thumbnail with the given ImageIcon
     * @param path
     * @param height
     * @param width
     */
    public void createThumbnail(URL path, int height, int width) {
        int originalHeight, originalWidth;
        double resizeRatioH, resizeRatioW, resizeRatio;
        ImageIcon originalImage = new ImageIcon(path);
        originalHeight = originalImage.getIconHeight();
        originalWidth = originalImage.getIconWidth();
        resizeRatioH = ((double) height / (double) originalHeight);
        resizeRatioW = ((double) width / (double) originalWidth);
        if(resizeRatioH>resizeRatioW){
            resizeRatio=resizeRatioW;
        }else{
            resizeRatio=resizeRatioH;
        }
        height = (int) (originalHeight * resizeRatio);
        width = (int) (originalWidth * resizeRatio);
        Image img = originalImage.getImage();
        Image scaledImg = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        thumbnail = new ImageIcon(scaledImg);
        thumbnail.setDescription(name);
    }

    public String getIP() {
        return ip;
    }

    public void setIP(String ip) {
        this.ip = ip;
    }

    @Override
    public String getAlbum() {
        return album;
    }

    @Override
    public void setAlbum(String album) {
        this.album = album;
    }
}


